#if !defined(LOG_TAG)
    #define LOG_TAG                    "tool"
#endif
#undef LOG_LVL
#if defined(TOOL_LOG_LVL)
    #define LOG_LVL                    TOOL_LOG_LVL
#endif

#include "tool.h"

int tool_ip_get(const char *pif_name, int af, char *pipaddr)
{
    struct ifaddrs *ifa_head;
    struct ifaddrs *ifa_cur;
    int result, addrstr_len;
    void *src;

    if (!pif_name || !pipaddr)
    {
        errno = EINVAL;
        return -1;
    }

    if (getifaddrs(&ifa_head) != 0)
        return -1;

    result = -1;
    for (ifa_cur = ifa_head; ifa_cur; ifa_cur = ifa_cur->ifa_next)
    {
        if (!ifa_cur->ifa_name)
            continue;

        if (!ifa_cur->ifa_addr)
            continue;

        if (ifa_cur->ifa_addr->sa_family != af)
            continue;

        if (strcmp(pif_name, (char *)ifa_cur->ifa_name) != 0)
            continue;

        if (af == AF_INET6)
        {
            addrstr_len = INET6_ADDRSTRLEN;
            src = &(((struct sockaddr_in6 *)ifa_cur->ifa_addr)->sin6_addr);
        }
        else
        {
            addrstr_len = INET_ADDRSTRLEN;
            src = &(((struct sockaddr_in *)ifa_cur->ifa_addr)->sin_addr);
        }

        if (inet_ntop(af, src, pipaddr, addrstr_len) != NULL)
            result = 0;

        break;
    }

    freeifaddrs(ifa_head);

    return result;
}


int tool_systime_set_if_over_60_second(int year, int month, int day, int hour, int minute, int second, int msecond)
{
	struct tm _tm;
	struct timeval tv, tnow;
	time_t timep;

    if(year&&month&&day)
    {

    }else
    {
        return -1;
    }

#if 0
	_tm.tm_year = year-1900;
	_tm.tm_mon  = month - 1;
	_tm.tm_mday = day;
	_tm.tm_hour = hour;
	_tm.tm_min  = minute;
	_tm.tm_sec  = second;

	timep = mktime(&_tm);
	tv.tv_sec = timep;
	tv.tv_usec = 0;

	int ret = gettimeofday(&tnow, NULL);
	if (ret == -1)
	{
		return ret;
	}

	if(abs(tnow.tv_sec-tv.tv_sec)>60)    // 防止连续设置系统时间。
	{
		if(settimeofday(&tv,NULL) < 0)
		{
			log_i("[error] set system error.%d-%d-%d,%d:%d:%d err%d",year, month,day,hour,minute,second,errno);
			return -1;
		}else
		{
			log_i("[info] set system success.%d-%d-%d,%d:%d:%d err%d",year, month,day,hour,minute,second,errno);
		}
	}
#else
    char str[100]={0};
	int ret = gettimeofday(&tnow, NULL);
	if (ret == -1)
	{
		return ret;
	}
    if(abs(tnow.tv_sec%60-second)>10)    // 防止连续设置系统时间。
    {
        sprintf(str, "date -s \"%d-%d-%d %d:%d:%d\"", year,month,day,hour,minute,second);
        //log_i("setetime %s",str);fflush(stdout);
        int ret = system(str);
        return 0;
    }

#endif
	return -2;
}


int tool_common_interval()
{
    struct timeval timeval_s, timeval_e;
    long long t_now = 0, t_warning = 0;
    static long long t_last_one = 0, t_last_n = 0;

    gettimeofday(&timeval_s, NULL);
    t_now = timeval_s.tv_sec * 1000+ timeval_s.tv_usec/1000;
    if(abs(t_now - t_last_n)>=20)
    {
        printf("%lld\r\n",t_now-t_last_n);
        t_last_n = t_now;
        
        /*for()
        {

        }*/
    }
    
}

